const  mysql  = require("mysql");
const connection = mysql.createPool({
  host: "localhost",
  user: "root",
  password: "jajt1234",
  database: "school",
  multipleStatements: true, //允许多条语句
  timezone: "SYSTEM", //优化时间格式
  connectionLimit: 100,
});

const executeQuery = (sql, values) => {
  return new Promise((resolve, reject) => {
    connection.getConnection((getConnectionErr, connection) => {
      if (getConnectionErr) {
        reject(getConnectionErr); // 错误处理：获取连接时出错
        return;
      }

      connection.query(sql, values, (queryErr, results) => {
        connection.release(); // 释放连接
        if (queryErr) {
          reject(queryErr); // 错误处理：查询执行时出错
        } else {
          resolve(results); // 成功返回结果
        }
      });
    });
  });
};


module.exports = {
  connection,
  executeQuery
};
